//
//  HMStatusViewCell.swift
//  WeiBo14
//
//  Created by LCH on 16/3/9.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
import SnapKit

let Margin:CGFloat = 8
let AvaterSize: CGFloat = 35

class HMStatusViewCell: UITableViewCell {

    
    var toolBarTopConstraint:Constraint?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
  
    
   //TODO:为什么这么初始化有问题
//    var statusViewModel:HMStatusViewModel = HMStatusViewModel()
    
    var statusViewModel: HMStatusViewModel? {
    
        didSet {
        
          
        statusView.statusViewModel = statusViewModel
        statusToolBar.statusViewModel = statusViewModel
            
            self.toolBarTopConstraint?.uninstall()
            
            if statusViewModel?.status?.retweeted_status != nil {
                
                // 给转发微博赋值
                
                retweetStatusView.statusViewModel = statusViewModel
                // 显示转发视图的微博
                retweetStatusView.hidden = false
                
                
                // 修改toolbar的约束
                statusToolBar.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.toolBarTopConstraint = make.top.equalTo(retweetStatusView.snp_bottom).constraint
                })
                
            }else {
                
                // 隐藏转发视图的微博
                retweetStatusView.hidden = true
                // 修改toolbar的约束
                statusToolBar.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.toolBarTopConstraint = make.top.equalTo(statusView.snp_bottom).constraint
                })
                
            }

        }
    
    
    }

    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .None
        contentView.backgroundColor = UIColor(white: 239/255, alpha: 1)
        
        contentView.addSubview(statusView)
        contentView.addSubview(statusToolBar)
        contentView.addSubview(retweetStatusView)
        
        statusView.snp_makeConstraints { (make) -> Void in
            
//            make.height.equalTo(200)
            
//            make.leading.trailing.top.equalTo(contentView)
            make.leading.trailing.equalTo(contentView)
            
            make.top.equalTo(contentView).offset(Margin)
        }
      
        statusToolBar.snp_makeConstraints { (make) -> Void in
            
           self.toolBarTopConstraint =  make.top.equalTo(retweetStatusView.snp_bottom).constraint
            make.leading.trailing.equalTo(statusView)
            make.height.equalTo(35)
            
            
        }
        retweetStatusView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(statusView.snp_bottom)
            make.leading.trailing.equalTo(statusView)
        }
        
        
        
        
 //TODO:这个计算的作用
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(statusToolBar)
        }
        
        
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 

    
    // 原创微博视图
   private lazy var statusView:HMStatusView = HMStatusView()
    
    // 地步转发评论赞
    private lazy var statusToolBar:HMStatusToolBar = HMStatusToolBar()
    
    // 转发
    private lazy var retweetStatusView:HMRetweetStatusView = HMRetweetStatusView()
    
 
}
